package com.midea.ai.air.ui.home.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.ui.home.SearchCityInChinese;
import com.midea.ai.air.ui.home.data.CityBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ouyangdi on 2016/4/19.
 * 城市搜索页面Fragment
 */
public class WeatherCitiesFragment extends BaseFragment implements TextWatcher{
    private List<CityBean> mLstSearch, mLstCur;
    EditText mEditText;

    ListView mListView;
    ListAdapter mAdapter;

    private GridView mGridHot;
    private List<String> mListHotCities;
    private GridAdapter mGridAdapter;
    private ViewGroup mHotCities;
    private ImageView mDelete;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_weather_cities, container, false);
        initData();
        initViews(view);
        return view;
    }

    private void initData() {
        mListHotCities = new ArrayList<>();
        mListHotCities.add("定位");
        mListHotCities.add("北京");
        mListHotCities.add("上海");

        mListHotCities.add("广州");
        mListHotCities.add("深圳");
        mListHotCities.add("天津");

        mListHotCities.add("武汉");
        mListHotCities.add("重庆");
        mListHotCities.add("沈阳");

        mListHotCities.add("成都");
        mListHotCities.add("西安");
        mListHotCities.add("青岛");

        mListHotCities.add("苏州");
        mListHotCities.add("宁波");
        mListHotCities.add("无锡");

        mListHotCities.add("香港");
        mListHotCities.add("台北");
        mListHotCities.add("澳门");

        mListHotCities.add("大连");
        mListHotCities.add("南京");
        mListHotCities.add("长沙");
    }
    private void initViews(View view) {
        mEditText = (EditText) view.findViewById(R.id.edit_input_city);
        mEditText.addTextChangedListener(this);

        mListView = (ListView)view.findViewById(R.id.listView_search_result);
        mAdapter = new ListAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(),"选择了"+ mLstCur.get(position).getCityName(), Toast.LENGTH_SHORT).show();
                mListView.setVisibility(View.GONE);
                mHotCities.setVisibility(View.VISIBLE);
            }
        });

        mGridHot = (GridView)view.findViewById(R.id.grid_view_hot_city);
        mGridAdapter = new GridAdapter();
        mGridHot.setAdapter(mGridAdapter);

        mHotCities = (ViewGroup) view.findViewById(R.id.layout_hot_cities);
        mDelete = (ImageView)view.findViewById(R.id.delete_add_name);
        mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditText.setText("");
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (mLstSearch == null) {
            mLstSearch = new ArrayList<>();
        }

        if(TextUtils.isEmpty(s)) {
            mLstCur.clear();
            mLstCur = null;
            mListView.setVisibility(View.GONE);
            mHotCities.setVisibility(View.VISIBLE);
            mDelete.setVisibility(View.GONE);
        } else {
            SearchCityInChinese.searchCityInChinese(getContext(), s.toString(), R.raw.weather_moji_city, mLstSearch);
            mLstCur = mLstSearch;
            mListView.setVisibility(View.VISIBLE);
            mHotCities.setVisibility(View.GONE);
            mDelete.setVisibility(View.VISIBLE);
            try {
                mAdapter.notifyDataSetChanged();
            } catch (Exception e) {

            }
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    /**
     * List Adapter
     */
    private class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mLstCur != null ? (mLstCur.size() > 0 ? mLstCur.size() : 1) : 0;
        }

        @Override
        public Object getItem(int position) {
            return Integer.valueOf(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean isEnabled(int position) {
            return (mLstCur != null && mLstCur.size() > 0) ? true : false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getActivity(),R.layout.item_search_cities, null);
                holder = new ViewHolder();
                holder.tvCity = (TextView)convertView.findViewById(R.id.tv_search_city);
                holder.botLine = convertView.findViewById(R.id.bottom_line);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            if (mLstCur != null && mLstCur.size() > 0) {
                CityBean city = mLstCur.get(position);
                holder.tvCity.setText(city.getCityName() + " " + city.getStateName());

                if(position == mLstCur.size()-1) {
                    holder.botLine.setVisibility(View.INVISIBLE);
                } else {
                    holder.botLine.setVisibility(View.VISIBLE);
                }
            }
            return convertView;
        }

        class ViewHolder {
            TextView tvCity;
            View botLine;
        }
    }

    /**
     * List Adapter
     */
    private class GridAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mListHotCities != null ? (mListHotCities.size() > 0 ? mListHotCities.size() : 1) : 0;
        }

        @Override
        public Object getItem(int position) {
            return Integer.valueOf(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean isEnabled(int position) {
            return (mListHotCities != null && mListHotCities.size() > 0) ? true : false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getActivity(),R.layout.item_hot_city, null);
                holder = new ViewHolder();
                holder.layout_hot_city = (ViewGroup) convertView.findViewById(R.id.layout_hot_city);
                holder.tvHotCityName = (TextView) convertView.findViewById(R.id.tv_hot_city_name);
                holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_location_icon);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            if (mListHotCities != null && mListHotCities.size() > 0) {
                final String city = mListHotCities.get(position);
                holder.tvHotCityName.setText(city);
                holder.layout_hot_city.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getActivity(),"选择了"+ city, Toast.LENGTH_SHORT).show();
                    }
                });
            }

            if(position == 0) {
                holder.ivIcon.setVisibility(View.VISIBLE);
            } else {
                holder.ivIcon.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        class ViewHolder {
            ViewGroup layout_hot_city;
            TextView tvHotCityName;
            ImageView ivIcon;
        }
    }
}
